package com.hy.web;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hy.domain.City;
import com.hy.domain.Hotel;

@Controller
public class HyJdController {

	@RequestMapping("hotel")
	public String hotel(Model model, @RequestParam(value = "city", defaultValue = "0", required = false) long city, @RequestParam(value = "page", defaultValue = "1", required = false) int page){
		int count = (int)Hotel.countHotels();
		int pages = count % 9 > 0 ? count / 9 + 1 : count / 9;
		if(page < 1 || page > pages){
			return "redirect:hotel.html?page=1";
		}
		
		
		int length = 9;
		int start = (int)((page - 1) * length);	
															//读取条数
		start = start < 0 ? 0 : start;
		
		List<Hotel> hotels = null;
		if(city != 0) {
			City c = City.findCity(city);
			hotels = Hotel.getSortEntitiesCity(c, start, length);

			model.addAttribute("city", c);

			List<Hotel> hs = Hotel.getSortEntitiesCity(c, 0, count);
			if(hs != null) {
				count = hs.size();
				pages = count % 9 > 0 ? count / 9 + 1 : count / 9;
			}
			model.addAttribute("city", c.getId());

		} else {
			hotels = Hotel.getReverseHotel(start, length);
			model.addAttribute("city", city);
		}
		
		int up = page > 1 && page < pages ? page - 1 : 1;				//上一页
		int next = page > 0 && page < pages ? page + 1 : 1;				//下一页
		
		model.addAttribute("hotels", hotels);
		model.addAttribute("pages", pages);
		model.addAttribute("up", up);
		model.addAttribute("next", next);
		model.addAttribute("page", page);
		return "hotel";
	}
	
	@RequestMapping("hotel_xq")
	public String hotelXq(Model model, @RequestParam(defaultValue = "1", required = false) long id){
		/*
		Hotel hotel = Hotel.findHotel(id);
		if(hotel == null){
			return "redirect:404";
		}
		model.addAttribute("hotel", hotel);
		return "hotel_xq";
		*/
		return "redirect:/hotel/view/" + id + ".html";
	}
	
}
